package com.apes.framework.condition;

import com.apes.framework.jpa.Connector;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.lib.PubToolUtil;
import com.apestech.api.condition.ConditionExec;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hup
 * @create 2019-04-02-9:15
 */
@Service("accountService")
public class AccountService extends PubToolUtil {

    @Autowired
    Connector connector;

    public JSONArray getCondition(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            String sql="SELECT ID, NAME FROM EXE_CONDITION WHERE 1=1 ";
            return getSession(connector.getConnection()).getForJSON().table("EXE_CONDITION", json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Object saveCondition(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            ConditionExec exec=new ConditionExec(getSession(connector.getConnection()), json.getString("ID"));
            return exec.saveCondtion();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void createTable(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            ConditionExec exec=new ConditionExec(getSession(connector.getConnection()), json.getString("ID"));
            exec.createTable(false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Object saveTable(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            ConditionExec exec=new ConditionExec(getSession(connector.getConnection()), json.getString("ID"));
            return exec.saveTable();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteTable(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            ConditionExec exec=new ConditionExec(getSession(connector.getConnection()), json.getString("ID"));
            exec.deleteTable(json, json.getString("tableName"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void executeTable(SimpleRequest param) throws RuntimeException {
        try {
            String body=(String)param.getData();
            JSONObject json= JSON.toJSON(body);
            ConditionExec exec=new ConditionExec(getSession(connector.getConnection()), json.getString("ID"));
            exec.execTable(json.getString("tableName"), json.getString("ID"), "exec", param.getPersonId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
